<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Goods;
use app\common\model\GoodsCategory;
use app\common\model\Carousel;
use think\Validate;
use think\Log;
use think\Db;
/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['get_carousel','get_new_goods','get_hot_goods','get_recommend_goods','get_category_goods','get_goods_category','get_keywords_goods'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功',['name'=>'硒乡坊','version'=>'1.0.0']);
    }
    public function get_carousel(){
        $carousel = Carousel::where('switch',1)->order('sort asc')->select();
        $this->success('请求成功',$carousel);
    }
    public function get_new_goods(){
        $param = $this->request->param();
        $page  = isset($param['page'])?$param['page']:1;
        $limit = isset($param['limit'])?$param['limit']:10;
        $goods = Goods::where('is_new',1)->order('sort asc')->page((int)$page,(int)$limit)->select();
        $this->success('请求成功',$goods);
    }
    public function get_hot_goods(){
        $param = $this->request->param();
        $page  = isset($param['page'])?$param['page']:1;
        $limit = isset($param['limit'])?$param['limit']:10;
        $goods = Goods::where('is_hot',1)->order('sort asc')->page((int)$page,(int)$limit)->select();
        $this->success('请求成功',$goods);
    }
    public function get_recommend_goods(){
        $param = $this->request->param();
        $page  = isset($param['page'])?$param['page']:1;
        $limit = isset($param['limit'])?$param['limit']:10;
        $goods = Goods::where('is_recommend',1)->order('sort asc')->page((int)$page,(int)$limit)->select();
        $this->success('请求成功',$goods);
    }
    public function get_goods_category(){
        $list = GoodsCategory::get_category_tree();
        
        $this->success('请求成功',$list);
    }
    public function get_category_goods(){
        $param = $this->request->param();
        if(empty($param['category_id'])){
            $this->error('参数错误');
        }
        $page  = isset($param['page'])?$param['page']:1;
        $limit = isset($param['limit'])?$param['limit']:10;
        $temp  = Goods::where('category_id',$param['category_id']);
        if(!empty($param['sales_count'])){
            $goods = $temp->order('sales_count',$param['sales_count']);
        }
        if(!empty($param['price'])){
            $goods = $temp->order('price',$param['price']);
        }
        $goods = $temp->page((int)$page,(int)$limit)->select();
        $this->success('请求成功',$goods);
    }
    public function get_keywords_goods(){
        $param = $this->request->param();
        if(empty($param['keywords'])){
            $this->error('参数错误');
        }
        $page  = isset($param['page'])?$param['page']:1;
        $limit = isset($param['limit'])?$param['limit']:10;
        $temp  = Goods::whereLike('sub_name|name',"%".$param['keywords']."%");
        if(!empty($param['sales_count'])){
            $goods = $temp->order('sales_count',$param['sales_count']);
        }
        if(!empty($param['price'])){
            $goods = $temp->order('price',$param['price']);
        }
        $goods = $temp->page((int)$page,(int)$limit)->select();
        $this->success('请求成功',$goods);
    }
    public function get_goods_detail(){
        $param = $this->request->param();
        if(empty($param['goods_id'])){
            $this->error('参数错误');
        }
        $goods  = Goods::with(['postage'])->where('id',$param['goods_id'])->find();
        $this->success('请求成功',$goods);
    }
}
